package com.twoGroup.boot.controller;

import com.twoGroup.boot.pojo.*;
import com.twoGroup.boot.service.imp.CommonServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
@Api(tags = "搜索接口")
public class CommonController {
    public static Logger logger = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private CommonServiceImpl commonService;

    /**
     * 搜索店铺
     *
     * @param shopName 输入店铺名称
     * @return shops
     */
    @ApiOperation(value = "搜索店铺", notes = "输入店铺名称")
    @PostMapping("/queryShops")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "shopName", value = "商品名称"),
                    @ApiImplicitParam(name = "pageNow", value = "当前页面"),
                    @ApiImplicitParam(name = "pageSize", value = "当前页面显示数量"),
            }
    )
    @ResponseBody
    public Map<String, Object> queryShops(String shopName, int pageNow, int pageSize) {
        Page<Shop> shopPage = commonService.queryShop(shopName, pageNow, pageSize);
        for (Shop shop : shopPage.getList()) {
            logger.info(String.valueOf(shop));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("shopPage", shopPage);
        return map;
    }

    /**
     * 搜索商品
     *
     * @param goodsName 输入商品名称
     * @return list集合
     */
    @ApiOperation(value = "搜索商品", notes = "输入商品名称")
    @PostMapping("/queryGoods")
    @ApiImplicitParams(
            {
                    @ApiImplicitParam(name = "goodsName", value = "商品名称"),
                    @ApiImplicitParam(name = "pageNow", value = "当前页面"),
                    @ApiImplicitParam(name = "pageSize", value = "当前页面显示数量"),
            }
    )
    @ResponseBody
    public Map<String, Object> queryGoods(String goodsName, int pageNow, int pageSize) {
        Page<GoodsAndShopAndGoodsTypeAndGoodsBrand> goodsPage = commonService.queryGoods(goodsName, pageNow, pageSize);
        for (GoodsAndShopAndGoodsTypeAndGoodsBrand list : goodsPage.getList()) {
            logger.info(String.valueOf(list));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("goodsPage", goodsPage);
        return map;
    }
}
